<?php

namespace Database\Seeders;

use App\Models\UserAuth;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class StudentSeeder extends Seeder
{
    /**
     * php artisan db:seed --class=StudentSeeder
     *
     * @return void
     */
    public function run()
    {
        // 清空数据表 后在添加
        $table = DB::table('student');
        $table->truncate();

        // 百家姓
        $time = time();
        $show = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许', '何', '吕', '施', '张', '孔', '曹', '严', '华', '金', '魏', '陶', '姜', '戚', '谢', '邹', '喻', '柏', '水', '窦', '章'];

        // 查询 机构认证的用户
        $users = (new UserAuth())->where('auth_type', 'organ')->get();
        $users = json_decode(json_encode($users), true);
        $count = count($users);

        // 填充
        for($x = 0; $x <= 100; $x++){
            // 参数
            $xing  = $show[mt_rand(0, count($show) - 1)];
            $_uKey = mt_rand(0, $count - 1);
            $_user = $users[$_uKey];

            $_row = $this->makeRow($xing, $time, $_user['uid'], $_user['id'], $x);
            // 保存数据到数据库中
            $table->insert($_row);
        }


    }


    // 生成数据
    protected function makeRow($xing, $time, $uid, $auth_id, $index)
    {
        $mobile = 13123456780 + $index;
        $_row   = [
            'uid'         => $uid,
            'auth_id'     => $auth_id,
            'person_uid'  => '0',
            'person_name' => $xing.getChar(mt_rand(1, 2)),  // 姓名
            'guardor'     => $xing.getChar(mt_rand(1, 2)),
            'head_img'    => 'https://img.shenlanhui.com/static/head_img/face_'.rand(1, 450).'.jpg',
            'mobile'      => $mobile,
            'sex'         => mt_rand(0, 2),
            'age'         => mt_rand(18, 50),
            'ctime'       => $time,
            'mtime'       => $time,
        ];

        return $_row;
    }

}
